//
//  XS_MineFeedBackVC.swift
//  XueDao
//
//  Created by Mac on 2020/12/17.
//

import UIKit

class XS_MineFeedBackVC: XS_BaseVC {

    @IBOutlet weak var collectionView: UICollectionView!
    var dataArray : [XS_FeedBackItemModel] = []
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        view.backgroundColor = #colorLiteral(red: 0.968627451, green: 0.968627451, blue: 0.968627451, alpha: 1)
        title = "反馈记录"
        collectionView.register(UINib.init(nibName: "XS_MineFeedBackItemCell", bundle: Bundle.main), forCellWithReuseIdentifier: "XS_MineFeedBackItemCell")
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.contentInset = UIEdgeInsets.init(top: 0, left: 0, bottom: .bottomHeight, right: 0)
        requestData()
    }
    
    func requestData(){
        XS_NetWork.requestJson("/helperChat/queryUserChat")
            .mapModels(type: XS_FeedBackItemModel.self) // 返回 模型数组 和原始数据
            .subscribe(onNext: { array, resp in
                self.dataArray = array
                self.collectionView.reloadData()
            }).disposed(by: disposeBag)
    }
}

extension XS_MineFeedBackVC : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "XS_MineFeedBackItemCell", for: indexPath) as! XS_MineFeedBackItemCell
        if indexPath.item < dataArray.count {
            cell.model = dataArray[indexPath.item]
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize.init(width: .screenWidth - 16 * 2, height: XS_MineFeedBackItemCell.getCellHeight(model: dataArray[indexPath.item]))
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 9
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 9
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets.init(top: 10, left: 16, bottom: 16, right: 16)
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let detailVC = XS_FeedBackDetailVC()
        detailVC.model = dataArray[indexPath.item]
        navigationController?.pushViewController(detailVC, animated: true)
    }
}
